/**
 *  根据指定的列名，对 对象数组排序
 *
 *  示例：
 *  var arr = [{name: "zlw", age: "24"}, {name: "wlz", age: "5"}];
 *  console.log(arr.sort(compare("age")))
 *
 * @param prop
 * @returns {Function}
 */
export function compare(prop) {
  return function (obj1, obj2) {
    var val1 = obj1[prop];
    var val2 = obj2[prop];
    if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
      val1 = Number(val1);
      val2 = Number(val2);
    }
    if (val1 < val2) {
      return -1;
    } else if (val1 > val2) {
      return 1;
    } else {
      return 0;
    }
  }
}
